package com.gxc.array;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
 * 给定坐标轴上的一组线段，线段的起点和终点均为整数并且长度不小于1，请你从中找到最少数量的线段，这些线段可以覆盖住所有线段。
 *
 * 输入描述
 * 第一行输入为所有线段的数量，不超过10000，
 * 后面每行表示一条线段，格式为”x,y"，x和y分别表示起点和终点，取值范围是[-105,105]
 * 输出描述
 * 最少线段数量，为正整数
 */
public class LineSegment {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        List<int[]> segments = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String str = scanner.nextLine();
            String[] split = str.split(",");
            segments.add(new int[]{Integer.parseInt(split[0]), Integer.parseInt(split[1])});
        }
        scanner.close();

        segments.sort(new Comparator<int[]>() {
            @Override
            public int compare(int[] a, int[] b) {
                return Integer.compare(a[0], b[0]);
            }
        });

        // 深拷贝 segments，防止合并区间时修改原始数据
        List<int[]> sortedSegments = new ArrayList<>();
        for (int[] segment : segments) {
            sortedSegments.add(new int[]{segment[0], segment[1]});
        }

        // 合并区间
        List<int[]> merged = new ArrayList<>();
        int[] current = sortedSegments.get(0);
        for (int i = 1; i < n; i++) {
            int[] next = sortedSegments.get(i);
            if (next[0] <= current[1]) {
                current[1] = Math.max(current[1], next[1]);
            } else {
                merged.add(current);
                current = next;
            }
        }
        merged.add(current);

        // 最小覆盖线段数
        int totalCount = 0;
        for (int[] merge : merged) {
            int L = merge[0], R = merge[1];
            int count = 0;
            int currentPoint = L;

            while (currentPoint < R) {
                int max = currentPoint;
                // 在所有能覆盖 `currentPoint` 的线段中，选终点最远的
                for (int[] segment : segments) {
                    if (segment[0] <= currentPoint && segment[1] > max) {
                        max = segment[1];
                    }
                }
                if (max == currentPoint) {
                    System.out.println(-1);
                    return;
                }
                count++;
                currentPoint = max;
            }
            totalCount += count;
        }
        System.out.println(totalCount);


    }
}
